package pokerclient.view.panels;

import java.awt.Color;
import javax.swing.GroupLayout;
import javax.swing.JComponent;

import javax.swing.WindowConstants;
import org.jdesktop.application.Application;

import pokerclient.core.Player;
import pokerclient.core.Utils;

import common.Card;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.LayoutStyle;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class CardPanel extends javax.swing.JPanel {
	private static final long serialVersionUID = 2483625235138033738L;
	private JLabel cardLabel1;
	private JLabel dealerLabel;
	private JLabel playerLabel;
	private JLabel actionLabel;
	private JLabel stackLabel;
	private JLabel cardLabel2;
	private Card emptyCard = new Card();

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new CardPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public CardPanel() {
		super();
		initGUI();
		this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		initEmptyCards();
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent) this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(229, 108));
			{
				cardLabel1 = new JLabel();
				cardLabel1.setBackground(Color.white);
				cardLabel1.setOpaque(true);

				cardLabel1.setName("cardLabel1");
				cardLabel1.setSize(72, 96);
			}
			{
				cardLabel2 = new JLabel();
				cardLabel2.setBackground(Color.white);
				cardLabel2.setOpaque(true);
				cardLabel2.setName("cardLabel2");
				cardLabel2.setSize(72, 96);
			}
			{
				dealerLabel = new JLabel();
				dealerLabel.setName("dealerLabel");
			}
			{
				playerLabel = new JLabel();
				playerLabel.setName("playerLabel");
			}
			{
				stackLabel = new JLabel();
				stackLabel.setName("stackLabel");
			}
			{
				actionLabel = new JLabel();
				actionLabel.setName("foldLabel");
			}
			thisLayout
					.setVerticalGroup(thisLayout
							.createSequentialGroup()
							.addGroup(
									thisLayout
											.createParallelGroup()
											.addComponent(
													cardLabel2,
													GroupLayout.Alignment.LEADING,
													0, 101, Short.MAX_VALUE)
											.addComponent(
													cardLabel1,
													GroupLayout.Alignment.LEADING,
													0, 99, Short.MAX_VALUE)
											.addGroup(
													GroupLayout.Alignment.LEADING,
													thisLayout
															.createSequentialGroup()
															.addGroup(
																	thisLayout
																			.createParallelGroup()
																			.addGroup(
																					GroupLayout.Alignment.LEADING,
																					thisLayout
																							.createSequentialGroup()
																							.addGap(12)
																							.addComponent(
																									playerLabel,
																									GroupLayout.PREFERRED_SIZE,
																									GroupLayout.PREFERRED_SIZE,
																									GroupLayout.PREFERRED_SIZE))
																			.addGroup(
																					GroupLayout.Alignment.LEADING,
																					thisLayout
																							.createSequentialGroup()
																							.addComponent(
																									dealerLabel,
																									GroupLayout.PREFERRED_SIZE,
																									GroupLayout.PREFERRED_SIZE,
																									GroupLayout.PREFERRED_SIZE)
																							.addGap(17)))
															.addPreferredGap(
																	LayoutStyle.ComponentPlacement.UNRELATED,
																	1,
																	Short.MAX_VALUE)
															.addComponent(
																	stackLabel,
																	GroupLayout.PREFERRED_SIZE,
																	GroupLayout.PREFERRED_SIZE,
																	GroupLayout.PREFERRED_SIZE)
															.addPreferredGap(
																	LayoutStyle.ComponentPlacement.UNRELATED,
																	1,
																	GroupLayout.PREFERRED_SIZE)
															.addComponent(
																	actionLabel,
																	GroupLayout.PREFERRED_SIZE,
																	15,
																	GroupLayout.PREFERRED_SIZE)
															.addGap(7)))
							.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED));
			thisLayout
					.setHorizontalGroup(thisLayout
							.createSequentialGroup()
							.addComponent(cardLabel1,
									GroupLayout.PREFERRED_SIZE, 72,
									GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(cardLabel2,
									GroupLayout.PREFERRED_SIZE, 72,
									GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
							.addGroup(
									thisLayout
											.createParallelGroup()
											.addGroup(
													GroupLayout.Alignment.LEADING,
													thisLayout
															.createSequentialGroup()
															.addComponent(
																	playerLabel,
																	GroupLayout.PREFERRED_SIZE,
																	GroupLayout.PREFERRED_SIZE,
																	GroupLayout.PREFERRED_SIZE)
															.addGap(0,
																	18,
																	Short.MAX_VALUE)
															.addComponent(
																	dealerLabel,
																	GroupLayout.PREFERRED_SIZE,
																	10,
																	GroupLayout.PREFERRED_SIZE))
											.addGroup(
													GroupLayout.Alignment.LEADING,
													thisLayout
															.createSequentialGroup()
															.addGap(0,
																	0,
																	Short.MAX_VALUE)
															.addComponent(
																	actionLabel,
																	GroupLayout.PREFERRED_SIZE,
																	62,
																	GroupLayout.PREFERRED_SIZE)
															.addGap(9))
											.addComponent(
													stackLabel,
													GroupLayout.Alignment.LEADING,
													0, 71, Short.MAX_VALUE)));
			Application.getInstance().getContext().getResourceMap(getClass())
					.injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void initEmptyCards() {
		cardLabel1.setText(null);
		cardLabel1.setIcon(emptyCard.getIcon());
		cardLabel2.setText(null);
		cardLabel2.setIcon(emptyCard.getIcon());
	}

	public void setBoxes(Player player, int pos, boolean started) {
		Card[] hand = player.getHand();
		if (started) {
			hand[0].makeHidden();
			hand[1].makeHidden();
		}

		if (hand != null) {
			if (hand[0] != null) {
				cardLabel1.setIcon(hand[0].getIcon());
			}
			if (hand[1] != null) {
				cardLabel2.setIcon(hand[1].getIcon());
			}
		}

		if (player.getNickname() != null) {
			playerLabel.setText(player.getNickname());
		}

		if (player.getAction() != null) {
			actionLabel.setText(Utils.getGameActionText(player.getAction()));
		}

		setStackLabel(player.getStack(), player.getAmountInPot());
	}

	public void setStackLabel(int stack, int amountInPot) {
		this.stackLabel.setText(amountInPot + "/" + stack);
	}

	public void setYellowBackground(boolean bool) {
		if (bool) {
			this.setBackground(Color.YELLOW);
		} else {
			setBackground(new Color(212, 208, 200));
		}

	}
}
